<template>
    <div class="app-container">
        
         <!-- 搜索表单 -->
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">            
            <el-form-item label="sni名称:" prop="sni">
                <el-input
                v-model="queryParams.sni"
                placeholder="请输入路由字符串"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="标签:" prop="tag">
                <el-input 
                v-model="queryParams.tag"
                placeholder="请输入Host字符串"
                clearable
                size="small"
                @key.enter.native="handleQuery"
                ></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        
        <!-- 表工具栏 新增删除等 -->
        <el-row :gutter="20" class="mb8">
            <el-col :span="1.5">
                <el-button
                type="primary"
                plain
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd"
                v-hasPermi="['kong:certificate:add']"                
                >新增</el-button>
            </el-col>

            <el-col :span="1.5">
                    <el-button
                    type="warning"
                    plain
                    icon="el-icon-download"
                    size="mini"
                    :loading="exportLoading"
                    @click="handleExport"
                    v-hasPermi="['kong:certificate:export']"
                >导出</el-button>
            </el-col>

            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList()"></right-toolbar>

        </el-row>


        <el-table v-loading="loading" :data="snis">
            <el-table-column label="名称" prop="name" align="center"></el-table-column>
            <el-table-column label="id" prop="id" align="center"></el-table-column>
            <el-table-column label="标签" prop="tags" align="center"></el-table-column>
            <el-table-column label="创建时间" prop="created_at" align="center" :formatter="dateFormat"></el-table-column>
            
            <el-table-column  label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-edit"
                        @click="handleUpdate(scope.row)"
                        v-hasPermi="['kong:sni:edit']"
                    >编辑</el-button>

                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-delete"
                        @click="handleDelete(scope.row)"
                        v-hasPermi="['kong:sni:remove']"
                    >删除</el-button>

                </template>
            </el-table-column>
        </el-table>


        
        <el-dialog :title="title" v-if="addOpen" :visible.sync="addOpen" append-to-body width="900px">
            <add-sni :operate="operate" :sniEntity="sinEntity" @callback="handleCloseDialogCallback"/>
        </el-dialog>


    </div>
</template>
<script>
import moment from 'moment'
import {SniList,deleteSni} from "@/api/kong/sni"
import addSni from './addSni.vue'
export default {
  components: { addSni },
    data(){
        return{
            snis: [],
            loading: false,
            // 查询条件
            queryParams:{
                pageNum: 1,
                pageSize: 10,
            },
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 导出遮罩层
            exportLoading: false,
            // 总数据行数
            total: 0,
            // 搜索显示标识
            showSearch: true,
            title: "新增",
            addOpen: false,
            sinEntity:null,
            operate: 'add',
        }
    },
    created(){
        this.getList();

    },
    methods: {
        getList(){
            SniList().then(res=>{
                console.log(JSON.stringify(res))
                this.snis=res.rows;
            }).catch(err=>{})
        },
        handleUpdate(row){
            this.sinEntity=row;
            this.title="编辑";
            this.operate="edit";
            this.addOpen=true;

        },
        handleDelete(row){
            this.$confirm('确定要删除吗？','警告',{             
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function(){
                return deleteSni(row.id);
            }).then(res =>{
                this.getList();
                this.msgSuccess("删除成功");
            })

        },
        handleAdd(){
            this.title="新增";
            this.operate="add";
            this.addOpen=true;
        },
        handleExport(){},
        handleQuery(){},
        resetQuery(){},
        //时间处理函数
        dateFormat(row, column){
            const _date = row[column.property]*1000; 
            if(_date ==undefined){return ''};
            return moment(_date).format("YYYY-MM-DD HH:mm:ss");
        },
        dateFormatLong(_date){
            if(_date ==undefined){return ''};
            return moment(_date*1000).format("YYYY-MM-DD HH:mm:ss");
        },
        handleCloseDialogCallback(payload){
            this.addOpen=false;
            this.getList();            
        }
    }
}
</script>